package be.pluyckx.batteryprogress.application;

import android.os.Environment;

import java.io.File;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import be.pluyckx.batteryprogress.database.BatteryHistoryDatabase;
import be.pluyckx.batteryprogress.logging.BatteryHistoryDump;
import be.pluyckx.batteryprogress.logging.LogcatDump;

/**
 * Created by Philip Luyckx on 15/08/13.
 */
public class Constants {
  public static final boolean DEBUG = false;

  public static final boolean PUBLIC_RELEASE = true;

  private static final String PREF_KEY_STORE_HISTORY = "store_history";
  private static final String PREF_KEY_STORE_LOGS = "store_logs";
  private static final String PREF_KEY_ART_AMOUNT = "art_amount";
  private static final String PREF_KEY_HISTORY_SIZE = "history_size";

  public static final String PREFERENCES_NAME = "be.pluyckx.batteryprogress";
  public static final DecimalFormat percentPerHourFormat = new DecimalFormat("0.0%/h");
  public static final DecimalFormat percentFormat = new DecimalFormat("0%");
  public static final DateFormat dateTimeFileFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
  public static final DateFormat dateTimeLongFormat = DateFormat.getDateTimeInstance();
  public static final DecimalFormat doubleZeroFormat = new DecimalFormat("00");
  public static final File dataDirectory = new File(Environment.getExternalStorageDirectory(), "/Android/data/be.pluyckx.batteryprogress");
  public static final LogcatDump logcatDump = new LogcatDump(dataDirectory);
  public static final BatteryHistoryDump historyDump = new BatteryHistoryDump(dataDirectory);
  static BatteryHistoryDump.Callback batteryHistoryDumpCallback = null;
  static LogcatDump.Callback logcatDumpCallback = null;
  static BatteryHistoryDatabase batteryHistoryDatabase = null;

  private Constants() {
  }

  public static BatteryHistoryDump.Callback getDefaultHistoryDumpCallback() {
    return batteryHistoryDumpCallback;
  }

  public static LogcatDump.Callback getDefaultLogcatDumpCallback() {
    return logcatDumpCallback;
  }

  public static BatteryHistoryDatabase getBatteryHistoryDatabase() {
    return batteryHistoryDatabase;
  }

  public static int getAverageRealTimeCalculatorAmount() {
    if (BatteryProgressApplication.getPreferences() != null) {
      return BatteryProgressApplication.getPreferences().getInt(PREF_KEY_ART_AMOUNT, 2);
    } else {
      return 2;
    }
  }

  public static void setAverageRealTimeCalculatorAmount(int amount) {
    if (BatteryProgressApplication.getPreferences() != null) {
      if (amount < 2) {
        amount = 2;
      }

      BatteryProgressApplication.getPreferences()
          .edit()
          .putInt(PREF_KEY_ART_AMOUNT, amount)
          .commit();
    }
  }

  public static int getHistorySize() {
    if (BatteryProgressApplication.getPreferences() != null) {
      return BatteryProgressApplication.getPreferences().getInt(PREF_KEY_HISTORY_SIZE, 100);
    } else {
      return 100;
    }
  }

  public static void setHistorySize(int size) {
    if (BatteryProgressApplication.getPreferences() != null) {
      if (size <= 2) {
        size = 2;
      }

      BatteryProgressApplication.getPreferences()
          .edit()
          .putInt(PREF_KEY_HISTORY_SIZE, size)
          .commit();
    }
  }

  public static void setStoreHistory(boolean store) {
    if (BatteryProgressApplication.getPreferences() != null) {
      BatteryProgressApplication.getPreferences()
          .edit()
          .putBoolean(PREF_KEY_STORE_HISTORY, store)
          .commit();
    }
  }

  public static boolean getStoreHistory() {
    if (BatteryProgressApplication.getPreferences() != null) {
      return BatteryProgressApplication.getPreferences().getBoolean(PREF_KEY_STORE_HISTORY, false);
    } else {
      return false;
    }
  }

  public static void setStoreLogs(boolean store) {
    if (BatteryProgressApplication.getPreferences() != null) {
      BatteryProgressApplication.getPreferences()
          .edit()
          .putBoolean(PREF_KEY_STORE_LOGS, store)
          .commit();
    }
  }

  public static boolean getStoreLogs() {
    if (BatteryProgressApplication.getPreferences() != null) {
      return BatteryProgressApplication.getPreferences().getBoolean(PREF_KEY_STORE_LOGS, false);
    } else {
      return false;
    }
  }
}
